var markdowntext;


new Vue({
    data() {
        return {
            article:null,
            draft:null,
            // text: "",
            commentText:"",
            tags: [],
            comments:[],
            centerDialogVisible: false,
            comment:{
                "articleId" : null,
                "user": {
                    "userId":null
                },
                "commentContent": null,
                "replyId": -1
            },
            currentReplyId:-1,
        }
    }, methods: {
        resetReply:function (){
          this.currentReplyId=-1;
        },
        publishComment:function (){
            var comment={
                "articleId":this.comment.articleId,
                "user": {
                     "userId":document.getElementById("userId").value
                },
                 "commentContent": this.commentText,
                 "replyId": this.currentReplyId
            }
            this.comments.push(comment);
            this.commentText="";
            axios.post("article/addComment.do",comment).then(resp => {
                window.location.reload();
            });
        }
    },
    mounted() {
        alert("draftId"+document.getElementById("draftId").value);
        alert("articleId"+this.$refs.articleId.value);

        /**
         * 如果草稿id不为空的话
         * 请求文章内容数据，展示到页面上
         */
        let draftId = document.getElementById("draftId").value;
        if (draftId!='null'){
            axios.get("article/readDraft.do?draftId="+draftId).then(resp => {
                this.draft = resp.data;
                markdowntext = this.draft.draftContent;
                $(function () {
                    var testView = editormd.markdownToHTML("test-markdown-view", {
                        markdown:markdowntext , // Also, you can dynamic set Markdown text
                    });
                });
            });
            return;
        }



        /**
         * 如果文章id不为空的话
         * 请求文章内容数据，展示到页面上
         */
        if (this.$refs.articleId.value!=null){
            axios.get("article/readOne.do?articleId="+this.$refs.articleId.value).then(resp => {
                // console.log(resp.data);
                this.article = resp.data;
                console.log("this.article:",this.article);
                // alert(this.article.articleContent);
                markdowntext = this.article.articleContent;
                // this.text = markdowntext;

                // alert(resp.data)
                // alert(markdowntext)
                $(function () {
                    var testView = editormd.markdownToHTML("test-markdown-view", {
                        markdown:markdowntext , // Also, you can dynamic set Markdown text
                    });
                });
            });
        }

        /**
         * 请求文章标签数据
         */
        axios.get("article/getTag.do?articleId="+this.$refs.articleId.value).then(resp => {
            // console.log(resp.data);
            this.tags = resp.data;
        });

        /**
         * 请求文章评论数据
         */
        axios.get("article/getComment.do?articleId="+this.$refs.articleId.value).then(resp => {
            // console.log(resp.data);
            this.comments = resp.data;
        });


    }
}).$mount("#app");